#pragma once

#include <string>
#include <memory>
#include <functional>
#include "MediaSession.h"

namespace jlh
{
	struct RtspUrlInfo
	{
		std::string url;
		std::string ip;
		uint16_t port;
		std::string suffix;
	};

	class Rtsp : public std::enable_shared_from_this<Rtsp>
	{
	public:
		Rtsp() {}
		virtual ~Rtsp() {}

		virtual void SetAuthConfig(std::string& realm, std::string& username, std::string& password)
		{
			this->realm_ = realm;
			username_ = username;
			password_ = password;
			has_auth_info_ = true;
			if (realm_ == "" || username_ == "")
			{
				has_auth_info_ = false;
			}
		}

		virtual void SetVersion(std::string& version)
		{
			version_ = version;
		}

		virtual std::string GetVersion()
		{
			return version_;
		}

		virtual std::string GetRtspUrl()
		{
			return rtsp_url_info_.url;
		}

		bool parseRtspUrl(std::string url)
		{
			char ip[100] = { 0 };
			char suffix[100] = { 0 };
			uint16_t port = 0;
			if (3 == sscanf_s(url.c_str() + 7, "%[^:]:%hu/%s", ip, 100, &port, suffix, 100))
			{
				rtsp_url_info_.port = port;
			}
			else if(2 == sscanf_s(url.c_str()+7,"%[^/]/%s", ip, 100, suffix, 100))
			{
				rtsp_url_info_.port = 554;
			}
			else
			{
				return false;
			}
			rtsp_url_info_.ip = ip;
			rtsp_url_info_.suffix = suffix;
			rtsp_url_info_.url = url;
			return true;
		}

	
		virtual MediaSessionPtr LookMediaSession(const std::string& suffix)
		{
			return nullptr;
		}

		virtual MediaSessionPtr LookMediaSession(MediaSessionId sessionId)
		{
			return nullptr;
		}

	protected:
		bool has_auth_info_ = false;
		std::string realm_;
		std::string username_;
		std::string password_;
		std::string version_;

		struct RtspUrlInfo rtsp_url_info_;
	};

}